package com.webamc.manager;

import com.webamc.common.enums.Fields;
import com.webamc.common.enums.Params;
import com.webamc.dao.ICustomerDao;
import com.webamc.model.Customer;
import com.webamc.model.CustomerInfo;
import com.webamc.util.CommonUtil;
import com.webamc.util.KeyUtil;
import org.apache.log4j.Logger;

import java.util.List;
import java.util.Map;

/**
 * Date: Mar 8, 2009
 * Time: 3:06:52 AM
 */
public class CustomerHandler implements ICustomerHandler {
    private Logger log = Logger.getLogger(CustomerHandler.class);
    private ICustomerDao customerDao;

    public void setCustomerDao(ICustomerDao customerDao) {
        this.customerDao = customerDao;
    }

    public boolean create(Customer u) {
        log.info("creating Customer = " + u);
        u.setCustomerId(KeyUtil.instance().getNextCustomerId());
        return customerDao.create(u);
    }

    public boolean update(Customer u) {
        log.info("updating Customer = " + u);
        return customerDao.update(u);
    }

    public Customer view(int id) {
        log.info("checking CustomerId= " + id);
        return customerDao.view(id);
    }

    public List<Customer> search(Customer u) {
        log.info("searching Customer= " + u);
        return customerDao.search(u);
    }

    public List<Customer> browse() {
        return customerDao.browse();
    }

    public void update(Map<String, String[]> m, Customer c) {
        c.setInfo(getInfo(m, c.getInfo()));
        c.setCategory(CommonUtil.getString(m, Fields.CUSTOMERCATEGORY, c.getCategory()));
        c.setStatus(CommonUtil.getString(m, Fields.CUSTOMERSTATUS, c.getStatus()));
        c.setStartTime(CommonUtil.getTime(m, Fields.STARTTIME, c.getStartTime()));
        c.setEndTime(CommonUtil.getTime(m, Fields.ENDTIME, c.getEndTime()));
    }

    public CustomerInfo getInfo(Map<String, String[]> m, CustomerInfo info) {
        if (info == null) {
            info = new CustomerInfo();
        }
        info.setFullName(CommonUtil.getString(m, Fields.FULLNAME, info.getFullName()));
        info.setDescription(CommonUtil.getString(m, Fields.DESC, info.getDescription()));
        info.setEmails(CommonUtil.getString(m, Fields.EMAIL, info.getEmails()));
        info.setContacts(CommonUtil.getString(m, Fields.CONTACT, info.getContacts()));
        info.setMobile(CommonUtil.getString(m, Fields.MOBILE, info.getMobile()));
        info.setAddress(CommonUtil.getString(m, Fields.ADDRESS, info.getAddress()));
        info.setCity(CommonUtil.getString(m, Fields.CITY, info.getCity()));
        info.setState(CommonUtil.getString(m, Fields.STATE, info.getState()));
        info.setGender(CommonUtil.getString(m, Fields.GENDER, info.getGender()));
        info.setDob(CommonUtil.getTime(m, Fields.DOB, info.getDob()));

        return info;
    }

    public Customer create() {
        return new Customer();
    }

    public String validate(Customer u) {
        return Params.VALID;
    }

    public Customer update(Map<String, String[]> m, int userId) {
        int id = CommonUtil.getInt(m, Fields.CUSTOMERID, 0);

        Customer p = view(id);
        update(m, p); //Param Validation
        validate(p);
        p.setLastUpdatedById(userId);
        p.setLastUpdated(System.currentTimeMillis());
        update(p);

        return p;
    }

    public Customer create(Map<String, String[]> m, int userId) {
        Customer p = create();
        validate(p);
        update(m, p); //Param Validation

        p.setLastUpdatedById(userId);
        p.setLastUpdated(System.currentTimeMillis());
        create(p);

        return p;
    }

    public List<Customer> search(Map<String, String[]> m) {
        Customer u = create();
        u.setCustomerId(CommonUtil.getInt(m, Fields.CUSTOMERID, u.getCustomerId()));
        update(m, u);

        return search(u);
    }

}
